<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once LIBRARIES_ENTITIES_DATABASE;
require_once LIBRARIES_ENTITIES_PROPERTIES;
require_once LIBRARIES_TOOLS_UTILITY;
require_once LIBRARIES_TOOLS_XTEMPLATE;
require_once LIBRARIES_TOOLS_PAGERBACKEND;

class PropertyController {

    public function invoke() {

        // function list
        $groupFunctions = $_SESSION['groupFunctions'];



        // directory path
        $dir = dirname(__FILE__);

        $view = '';
        if (isset($_GET['view'])) {
            $view = $_GET['view'];
        }

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }



        $task = '';
        if (isset($_GET['task'])) {
            $task = $_GET['task'];
        }


        $rowToView = 10;
        if (isset($_POST["limit"])) {
            $rowToView = $_POST["limit"];
        }


        switch ($action) {
            case 'remove':
                $values = $_POST["cid"];
                $result = $this->deleteProperty($values);
                if ($result == OK) {
                    Utility::messageBox('Xóa thành công');
                    Utility::redirectCurrentPage();
                }
                break;
            case 'add':
            case 'addAndExit':

                // property name
                $name = $_POST['txtName'];
                $result = $this->addProperty($name);
                if ($result == OK) {
                    if ($action == 'add') {
                        Utility::messageBox('Thêm thành công');
                        Utility::redirectCurrentPage();
                    } else {
                        Utility::redirect(".?com=com_property&task=view");
                    }
                }
                break;
            case 'edit':
            case 'editAndExit':

                // ID
                $ID = $_POST['hdID'];

                // property name
                $name = $_POST['txtName'];
                $result = $this->updateProperty($ID, $name);

                if ($result == OK) {
                    if ($action == 'edit') {
                        Utility::messageBox('Sửa thành công');
                        Utility::redirectCurrentPage();
                    } else {
                        Utility::redirect(".?com=com_property&task=view");
                    }
                }
                break;
            case 'cancel':
                Utility::redirect(".?com=com_property&task=view");
                break;
        }








        if ($task == 'view') {
            // check view permission
            if (Utility::checkPermission('view', $groupFunctions, ITEM_PROPERTY_MANAGEMENT)) {

                $viewFile = $dir . '/views/property_list.html';
                $xtemplate = new XTemplate($viewFile);

                // CHECK PERMISSION
                // check add button
                if (Utility::checkPermission('add', $groupFunctions, ITEM_PROPERTY_MANAGEMENT)) {
                    $xtemplate->parse('main.add_button');
                }
                // check delete button
                if (Utility::checkPermission('delete', $groupFunctions, ITEM_PROPERTY_MANAGEMENT)) {
                    $xtemplate->parse('main.delete_button');
                }


                $indexCheckBox = 1;

                $rurrentPage = 1;
                if (isset($_POST["page"])) {
                    $rurrentPage = $_POST["page"];
                }


                // chỉ mục (số thứ tự của bài viết) bài viết
                $index = 1;

                $properties = $this->getPropertyList();

                $pager = PagerBackEnd::getPagerData(count($properties), $rowToView, $rurrentPage);


                if ($properties != null) {

                    $indexCheckBox = 1;

                    foreach ($properties as $property) {

                        if ($index > $pager->offset && $index <= $pager->limit) {

                            $xtemplate->assign('INDEX', $index);

                            $xtemplate->assign('INDEX_CHECKBOX', $indexCheckBox);

                            $xtemplate->assign('ID', $property->getID());
                            $xtemplate->assign('NAME', $property->getName());

                            // check update permission                            
                            if (Utility::checkPermission('update', $groupFunctions, ITEM_PROPERTY_MANAGEMENT)) {
                                $xtemplate->parse('main.row.update_button');
                            } else {
                                $xtemplate->parse('main.row.update_button_disable');
                            }


                            $xtemplate->parse('main.row');

                            $indexCheckBox++;
                        }
                        $index++;
                    }
                }

                $xtemplate->assign('LIMIT_LIST', Utility::getLimitList($rowToView));
                $xtemplate->assign('ROW_TO_VIEW', $rowToView);
                $xtemplate->assign('CURRENT_PAGE', $rurrentPage);
                $xtemplate->assign('AMOUNT_OF_PAGE', $pager->numPages);
                $xtemplate->assign('PAGING_HTML', $pager->paging);
                $xtemplate->parse('main');
                $xtemplate->out('main');
            } else {
                echo ERROR_PERMISSION;
            }
        } else if ($task == 'add') {

            // check add permission
            if (Utility::checkPermission('add', $groupFunctions, ITEM_PROPERTY_MANAGEMENT)) {
                $xtemplate = new XTemplate($dir . '/views/property_add.html');
                $xtemplate->parse('main');
                $xtemplate->out('main');
            } else {
                echo ERROR_PERMISSION;
            }
        } else if ($task == 'edit') {

            // check add permission
            if (Utility::checkPermission('update', $groupFunctions, ITEM_PROPERTY_MANAGEMENT)) {
                $ID = '';
                if (isset($_GET['ID'])) {
                    $ID = $_GET['ID'];
                }

                $property = $this->getPropertyByID($ID);

                if ($property != null) {

                    $viewFile = $dir . '/views/property_edit.html';

                    $xtemplate = new XTemplate($viewFile);

                    $xtemplate->assign('ID', $property->getID());
                    $xtemplate->assign('NAME', $property->getName());

                    $xtemplate->parse('main');
                    $xtemplate->out('main');
                } else {
                    Utility::redirect('.?com=com_property&task=view');
                }
            } else {
                echo ERROR_PERMISSION;
            }
        }
    }

    // get list of video
    private function getPropertyList() {
        $db = new Database();
        try {
            $property = new Properties();
            return $property->getList($db);
        } catch (PDOException $exc) {
            $db->rollback();
            echo $exc->getMessage();
            exit();
        }
    }

    // add a video
    private function addProperty($name) {
        $db = new Database();
        try {
            $property = new Properties();
            $property->setName($name);
            $property->add($db);

            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            $db->rollback();
            echo $exc->getMessage();
            exit();
        }
    }

    // delete many video
    private function deleteProperty($values) {
        $db = new Database();
        try {
            for ($i = 0; $i < count($values); $i++) {
                $property = new Properties();
                $property->setID($values[$i]);
                $property->delete($db);
            }
            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            $db->rollback();
            echo $exc->getMessage();
            exit();
        }
    }

    // add a video
    private function updateProperty($ID, $name) {
        $db = new Database();
        try {
            $property = new Properties();
            $property->setID($ID);
            $property->setName($name);
            $property->update($db);
            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            $db->rollback();
            echo $exc->getMessage();
            exit();
        }
    }

    private function getPropertyByID($ID) {
        $db = new Database();
        try {
            $property = new Properties();
            $property->setID($ID);
            return $property->getByID($db);
        } catch (PDOException $exc) {
            $db->rollback();
            echo $exc->getMessage();
            exit();
        }
    }

}
